package com.returnsoft.softphone.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.softphone.dto.CampaignDto;
import com.returnsoft.softphone.dto.SupervisorDto;
import com.returnsoft.softphone.eao.CampaignEao;
import com.returnsoft.softphone.eao.SupervisorEao;
import com.returnsoft.softphone.entity.Campaign;
import com.returnsoft.softphone.entity.Supervisor;
import com.returnsoft.softphone.exception.ConversionExcepcion;
import com.returnsoft.softphone.exception.CCenterExcepcion;
import com.returnsoft.softphone.exception.EaoExcepcion;
import com.returnsoft.softphone.util.Conversion;

/**
 * Session Bean implementation class SupervisorBean
 */
@Stateless
@LocalBean
public class SupervisorBean implements SupervisorInterface {

	@EJB
	private SupervisorEao supervisorEao;
	@EJB
	private CampaignEao campaignEao;
	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public SupervisorBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public SupervisorDto findSupervisor(Integer userId) throws CCenterExcepcion {
		try {
			Supervisor supervisor = supervisorEao.findSupervisor(userId);
			if (supervisor!=null && supervisor.getId()>0) {
				SupervisorDto supervisorDto = conv.fromSupervisor(supervisor);
				return supervisorDto;	
			}else{
				return null;	
			}
			
		} catch (EaoExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new CCenterExcepcion(e.getMessage());
		}
	}

	@Override
	public List<CampaignDto> findCampaigns(Integer supervisorId)
			throws CCenterExcepcion {
		try {
			List<Campaign> campaigns = campaignEao
					.findBySupervisor(supervisorId);
			List<CampaignDto> campaignsDto = new ArrayList<CampaignDto>();
			if (campaigns!=null && campaigns.size()>0) {
				for (Campaign campaign : campaigns) {
					CampaignDto campaignDto = conv.fromCampaign(campaign);
					campaignsDto.add(campaignDto);
				}
			}
			return campaignsDto;

		} catch (EaoExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new CCenterExcepcion(e.getMessage());
		}

	}
	
	@Override
	public List<SupervisorDto> find(List<Integer> campaignsId)
			throws CCenterExcepcion {

		try {
			
			List<SupervisorDto> supervisors = new ArrayList<SupervisorDto>();
			
			if (campaignsId!=null && campaignsId.size()>0) {
				supervisors = supervisorEao.findByCampaigns(campaignsId);
			}
			
			return supervisors;

		} catch (EaoExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new CCenterExcepcion(e.getMessage());
		}
	}
	

}
